/**
 *
 */
package com.example.sunsg.okhttptest.customview;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.Region.Op;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

/**
 * @author xiaozhongzhong
 */
public class RoundLayout extends LinearLayout {
    protected static final boolean DEBUG = true;
    protected int mCoverColor;
    private final Rect mRect = new Rect();
    private Paint mCoverPaint;
    private GradientDrawable mGradientDrawable;
    private int mRadius = 0;
    private Path mPath;

    @SuppressLint("NewApi")
    public RoundLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public RoundLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RoundLayout(Context context) {
        super(context);
        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (getMeasuredWidth() != 0) {
            if (mRadius <= 0) {
                //zhaopeng add  默认为view本身大小的一半
                mRadius = getMeasuredWidth() / 2;
            }
            mPath = new Path();
            mPath.addCircle(mRadius, mRadius, mRadius, Path.Direction.CCW);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
    }

    @Override
    public void draw(Canvas canvas) {
        mGradientDrawable.setBounds(mRect);
        mGradientDrawable.draw(canvas);

        if (mPath != null) {
            canvas.clipPath(mPath, Op.REPLACE);
        }
        canvas.drawColor(mCoverColor);
        super.draw(canvas);
    }

    public int getCoverColor() {
        return mCoverColor;
    }

    public void setCoverColor(int coverColor) {
        mCoverColor = coverColor;
    }

    public void setGradientDrawable(GradientDrawable gradientDrawable){
        mGradientDrawable = gradientDrawable;
        mGradientDrawable.setShape(GradientDrawable.OVAL);
        mGradientDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);
        postInvalidate();
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void init() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
        mCoverPaint = new Paint();
        mCoverPaint.setAntiAlias(true);
        mGradientDrawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[]{0x00FFFFFF,
                0x00FFFFFF});
        mGradientDrawable.setShape(GradientDrawable.OVAL);
        mGradientDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);
        //		mRadius = ScreenUtil.getDimenPx(getContext(),
        //				R.dimen.size_clean_main_wave_layout) / 2;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mRect.right = w;
        mRect.bottom = h;
        mRadius = 0;//zhaopeng add
    }
}
